package com.chainyoung.component_tmperson.ui.mvp.presenter;

import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.appcommon.entity.HdWalletTokenCache;
import com.chainyoung.common.appcommon.hdwallet.TokenBalance;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.ui.mvp.contract.HdWalletContract;
import com.chainyoung.component_tmperson.ui.mvp.contract.HdWalletTokenListContract;

import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

public class HdWalletTokenListPresenter extends BasePresenter<HdWalletTokenListContract.Model, HdWalletTokenListContract.View> {


    @Inject
    public HdWalletTokenListPresenter(HdWalletTokenListContract.Model model, HdWalletTokenListContract.View rootView) {
        super(model, rootView);
    }


    public void getAllTokenTransfers(String addr) {
        addSubscribe(mModel.getAllTokenTransfers(addr)
                .compose(RxUtils.<String>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<String>(mView) {
                    @Override
                    public void onNext(String data) {
                        mView.onGetAllTokenTransfersSuccessed(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.onGetAllTokenTrasferFailed();
                        super.onError(e);
                    }
                }));
    }

    public void getTokenBalance(final String contractAddr, String addr) {
        addSubscribe(mModel.getTokenBalance(contractAddr,addr)
                .compose(RxUtils.<TokenBalance>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<TokenBalance>(mView) {
                    @Override
                    public void onNext(TokenBalance data) {
                        mView.onGetTokenBalanceSuccessed(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.onGetTokenBalanceFailed(contractAddr);
                        super.onError(e);
                    }
                }));
    }


    public void getAllHdWalletTokenCaches(String address) {
        addSubscribe(mModel.getAllHdWalletTokenCaches(address)
                .compose(RxUtils.<List<HdWalletTokenCache>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<HdWalletTokenCache>>(mView) {
                    @Override
                    public void onNext(List<HdWalletTokenCache> data) {
                        mView.onGetAllHdWalletTokenCaches(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));


    }

    public void addHdWalletTokenCache(HdWalletTokenCache item) {
        addSubscribe(mModel.addHdWalletTokenCache(item)
                .compose(RxUtils.<HdWalletTokenCache>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<HdWalletTokenCache>(mView,false) {
                    @Override
                    public void onNext(HdWalletTokenCache data) {
                        mView.onAddHdWalletCacheTokenSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }

    public void getTokenNames(){
        addSubscribe(mModel.getTokenNames()
                .compose(RxUtils.<String>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<String>(mView) {
                    @Override
                    public void onNext(String data) {
                        mView.onGetAllTokenNamesSuccessed(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }



}
